using System;
using Godot;
using KludgeBox;

namespace VectorSurvivors;

public partial class Curtain : Control
{
	[Export] [NotNull] public GameState StateContainer { get; private set; }
	[Export] [NotNull] public TextureRect GradientTexture { get; private set; }
	[Export] [NotNull] public TextureRect FillTexture { get; private set; }

	private double Width => Size.X;
	private double Height => Size.Y;

	/// <inheritdoc />
	public override void _Ready()
	{
		NotNullChecker.CheckProperties(this);
		Visible = true;
		GradientTexture.Position = Vec(Width, 0);
		FillTexture.Position = Vec(Width * 2, 0);
	}

	public void ChangeState(PackedScene newState, Action<Node> callback = null)
	{
		SetAnchorsPreset(LayoutPreset.FullRect);
		GradientTexture.Position = Vec(Width, 0);
		FillTexture.Position = Vec(Width * 2, 0);
		
		var tween = GetTree().CreateTween().SetEase(Tween.EaseType.InOut).SetTrans(Tween.TransitionType.Cubic);
		tween.TweenProperty(this, "position:x", -Width * 2, 0.5);
		tween.TweenCallback(Callable.From(() => StateContainer.ChangeStateTo(newState.Instantiate())));
		if(callback is not null)
			tween.TweenCallback(Callable.From(() => callback.Invoke(StateContainer.GetCurrentState())));
		tween.TweenProperty(this, "position:x", 0, 0.5);
	}
}